package com.exchange.controller;

import com.exchange.config.KettleHelper;
import com.exchange.config.RabbitmqConfig;
import com.exchange.service.JobService;
import com.exchange.utils.result.CommonResult;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.pentaho.di.core.exception.KettleException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RefreshScope
@RequestMapping("/kettle/job")
@Api(value = "作业管理", tags = "作业管理接口")
public class JobController {

    @Autowired
    private JobService jobService;

    @PostMapping("/execRepositoryJobs")
    @ApiOperation("执行作业")
    public CommonResult execRepositoryJobs(@RequestParam("jobName") String jobName) throws KettleException {
        return jobService.execRepositoryJobs(KettleHelper.getEnviroments().getRepository(), jobName);
    }

    @RabbitListener(queues = {RabbitmqConfig.QUEUE_INFORM_JOB})
    public void getMsgJob(String jobName, Message message, Channel channel) throws KettleException, IOException {
       System.out.println("============================="+message.getBody().toString());
       try{
           jobService.execRepositoryJobs(KettleHelper.getEnviroments().getRepository(), jobName);
       }catch (Exception e){
           e.printStackTrace();
       }
    }

}
